package com.woniuxy.book.util;

import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;

public class DateUtil {

    public static boolean compareDateByString(String date1, String date2) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date d1 = format.parse(date1);
            Date d2 = format.parse(date2);
            if(d1.getTime()>d2.getTime()){
                return true;
            }else{
                return false;
            }
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    public static String Date2String(Date date){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }

    public static String DateTime2String(Date date){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(date);
    }

    public static String LocalDateTime2String(LocalDateTime date){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(date);
    }

    public static String DateTime2String(String dateString){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = format.parse(dateString);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        return format.format(date);
    }

    public static String DateTime2StringAddEightHour(String dateString){
        if(StringUtils.hasLength(dateString)){
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX");
            Date date = null;
            try {
                date = format.parse(dateString);
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
            return format.format(date);
        }else {
            return dateString;
        }

    }
}